package util;

import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import com.example.administrator.text.AppointTimeActivity;
import com.example.administrator.text.HomeActivity;
import com.example.administrator.text.R;
import com.google.gson.Gson;

import org.apache.log4j.Logger;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import app.MyApplication;
import data.SharedPreferencesUtils;
import entity.Networking;
import util.BReceiverUtil.AlarmReceiver;
import view.diaLogView.CrowdRetunDialog;
import view.diaLogView.EntryPromptDialog;
import view.diaLogView.MemberDialog;
import view.diaLogView.NetworkingDialog;
import view.diaLogView.RetunDialog;
import view.diaLogView.UploadDialog;

import static app.MyApplication.mFWQIP;
import static com.example.administrator.text.AppointTimeActivity.mRenew;

/**
 * Created by liuxiaobin on 2017/3/23
 * 联网 断网类的类
 */

public class NetWorkstateUtil {

    private static NetWorkstateUtil mNetWorkstateUtil;
    private NetworkingDialog mNetworkingDialog;
    private Logger mLogger = Logger.getLogger(NetWorkstateUtil.class);
    private int mNetWorkstateFail = 0;
    private boolean isNetWorkstae; //记录有没有联网
    private NetWorkstateUtil(){

    }

    public static NetWorkstateUtil getInstance(){
        if(mNetWorkstateUtil == null){
            synchronized (NetWorkstateUtil.class){
                if(mNetWorkstateUtil == null){
                    return mNetWorkstateUtil = new NetWorkstateUtil();
                }
            }
        }
        return mNetWorkstateUtil;
    }

    /**
     * 联网
     */
    public void getNetworking(final Context context, final UploadDialog uploadDialog, final String type) {

        if(getmNetWorkstateFail() == 0){
            if(!type.equals(HomeUtil.ZHUYENETWORKING)){
                if(uploadDialog !=null){
                    uploadDialog.show();
                }
            }
        }
        final String token = (String) SharedPreferencesUtils.getParam(context, HomeUtil.mTokenDB, "");
        final String id = (String) SharedPreferencesUtils.getParam(context, HomeUtil.mUseridDB, "");
        final HashMap<String,Object> mMap = new HashMap<>();
        mMap.put("siteAreaid",MyApplication.getApp().getSiteAreaid());       //项目ID
        mMap.put("userid",id);
        String ip = getInformation.getNewGetInformation(context).getI_IP();
        String mac = getInformation.getNewGetInformation(context).getIMAC();
        mLogger.info("联网参数--->ip--"+ip+"..mac"+mac);
        if(ip.equals("") || mac.equals("")){
            NetworkChangeAgent.newInstance(context).getNetWorkstatePortal(new NetworkChangeAgent.OnNetworkParameterClickListener() {
                @Override
                public void onClick(String ip) {
                    mLogger.info("联网参数--->ip--"+ip+"..mac"+getInformation.getNewGetInformation(context).getIMAC());
                    if(StringUtil.getStringUtilNew().judgeNetWorkstateParameter(context,getInformation.getNewGetInformation(context).getI_IP(),ip)){
                         if(getInformation.getNewGetInformation(context).getI_IP().equals("")){
                             mMap.put("ip",ip);
                         }else {
                             mMap.put("ip",getInformation.getNewGetInformation(context).getI_IP());
                         }
                        mMap.put("mac",getInformation.getNewGetInformation(context).getIMAC());
                        mMap.put("visitType",1);
                        networking(context,mMap,token,id,uploadDialog,type);
                    }else {
                        showWiFiView();
                        if(uploadDialog !=null){uploadDialog.dismiss();}
                    }
                }
            });
        }else {
            mMap.put("ip",ip);
            mMap.put("mac",mac);
            mMap.put("visitType",1);
            LogUtil.eE("","深职院联网"+ip);
            if(StringUtil.getStringUtilNew().judgeMac(getInformation.getNewGetInformation(context).getWiFiMac())){
                networking(context,mMap,token,id,uploadDialog,type);
            }else {
                networking(context,mMap,token,id,uploadDialog,type);
            }
        }
    }

    /**
     * 联网
     */
    private void networking(final Context context,HashMap<String,Object> map,String token,String id,final UploadDialog uploadDialog,final String netWorksttype){
        String stringJson = StringUtil.getStringUtilNew().hashMapToJson(map); //参数包18:dc:56:12:12:1b
        long time = StringUtil.getStringUtilNew().getTimeCurrent();
        String stirngSign = StringUtil.getStringUtilNew().getSign(Url.mStirngNetwork_linkedNet,stringJson,token,id,time);
        Map map1 = StringUtil.getStringUtilNew().getMapNew(context,stringJson,token,id,Url.mStirngNetwork_linkedNet,stirngSign,time);
        String fwq = (String) SharedPreferencesUtils.getParam(context,HomeUtil.mFWQ,"");
        LogUtil.eE("","登入ip    "+getInformation.getNewGetInformation(context).getI_IP());
        setNetWorkstae(true);
        mLogger.info("networking参数--->stringJson--"+stringJson+"fwq"+fwq+"...NF_callid..."+map1.get("NF_callid"));
        GreenTreeNetworkUtil.getInstance().doPost(context,fwq,15000,map1,new RequestDataCallback() {
            @Override
            public void onSuccess(String response) {
                Log.e("response", "          " + response);
                mLogger.info("networking结果--->onSuccess--->String--"+response);
                Gson gson = new Gson();
                final Networking networking = gson.fromJson(response,Networking.class);
                if (networking.getCode() == 1){
                    if(networking.getConnect() == 1){
                        if(getmNetWorkstateFail() == 0){
                            if(uploadDialog !=null){
                             uploadDialog.getStringShow("验证中");}
                         }
                             HomeUtil.getHemeUtilNew().domainUtil(context,2500,new HomeUtil.OnHomeUtilClickListener() {
                                        @Override
                                        public void onItemClick(String type) {
                                            if(type.equals("ok")){
                                                if(uploadDialog != null){ uploadDialog.dismiss();}
                                                szuNetworkstateSuccess(context,networking.getNetworkingTime(),netWorksttype);
                                            }else {
                                                showWiFiView();
                                                if(uploadDialog !=null){uploadDialog.dismiss();}
                                                Toast.makeText(context,"联网失败，假成功",Toast.LENGTH_SHORT).show();
                                            }
                                        }
                         });
                    }else if(networking.getConnect() == 2){
                        showWiFiView();
                        if(uploadDialog !=null){uploadDialog.dismiss();}
                        int firstfree = networking.getFirstfreeHimt().getFirstfree();
                        int type = StringUtil.getStringUtilNew().getjudeNetWorksteMode(networking.getBandingHimt(),networking.getErweimaHimt(),
                                firstfree,networking.getComboHimt(),networking.getVipHimt(),networking.getVipnum());
                        mLogger.info("networking--->onSuccess--->type--"+type);
                        switch (type) {
                            case 2:
                                noPackage(context);
                                break;
                            case 1:
                                moneyNetWorkstate(context);
                                break;
                            case 3:
                                JumpActivityUtils.getInstance().jumpAppointTime(context, AppointTimeActivity.mNetWorkstate, "");
                                break;
                            case 4:
                                networkingJudge(context,response);
                                break;
                            case 0:
                                Toast.makeText(context,"没有可联网资源", Toast.LENGTH_SHORT).show();
                                break;
                        }
                    }else{
                        showWiFiView();
                        if(uploadDialog !=null){uploadDialog.dismiss();}
                        Toast.makeText(context,"联网失败:connect="+networking.getConnect(),Toast.LENGTH_SHORT).show();
                    }

                }else if(networking.getCode() == -1){
                    showWiFiView();
                    if(uploadDialog !=null){uploadDialog.dismiss();}
                    Toast.makeText(context, R.string.FWQ,Toast.LENGTH_SHORT).show();
                }else if(networking.getCode() == -2){
                    showWiFiView();
                    if(uploadDialog !=null){uploadDialog.dismiss();}
                    CrowdRetunDialog crowdRetunDialog = new CrowdRetunDialog(context);
                    crowdRetunDialog.show();
                }else{
                    showWiFiView();
                    if(uploadDialog !=null){uploadDialog.dismiss();}
                    Toast.makeText(context,R.string.FWQ,Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Throwable error) {
                if(uploadDialog !=null){uploadDialog.dismiss();}
                showWiFiView();
                if(mNetworkingDialog != null){mNetworkingDialog.dismiss();}
                mLogger.info("onFailure()--->Throwable"+error.getMessage());
                StringUtil.getStringUtilNew().getExceptionCode(context, error.toString());
            }
        });
    }

    /**
     * 提示去购买
     */
    private void noPackage(Context context){
        String id = (String) SharedPreferencesUtils.getParam(context, HomeUtil.mUseridDB,"");
        int mVIPCombonum = (int) SharedPreferencesUtils.getParam(context, HomeUtil.mVIPCombonum,1);
        String url = Url.mATIAIRIP +Url.getUrlNew().vipdePosit+"?userid="+id+"&vipid="+mVIPCombonum+"&pattern="+MyApplication.getApp().getmPattern();
        JumpActivityUtils.getInstance().jumpWebView(context,url,"套餐购买");
    }

    /**
     * 深职院的处理
     */
    private void networkingJudge(Context context,String networking){
        JumpActivityUtils.getInstance().jumpWeBinDingPhone(context,networking,"HOME");
    }

    /**
     * 用钱联网
     */
    public void moneyNetWorkstate(Context context){
           MemberDialog memberDialog = new MemberDialog(context,1);
           memberDialog.show();

    }

    /**
     * 深大类联网
     * phone 账号
     * pwd  密码
     */
    public void getNetworkingSZ(final Context context, final String phone, final String pwd, final UploadDialog uploadDialog, final boolean state,
                                final EntryPromptDialog entryPromptDialog,final String sessionid) {
       if(getmNetWorkstateFail() == 0){ uploadDialog.show();}
        final String token = (String) SharedPreferencesUtils.getParam(context, HomeUtil.mTokenDB, "");
        final String id = (String) SharedPreferencesUtils.getParam(context, HomeUtil.mUseridDB, "");
        final HashMap<String, Object> map = new HashMap<>();
        map.put("userid",id);
        map.put("account",phone);
        map.put("accountPwd",pwd);
        map.put("sessionid",sessionid);
        String mac = getInformation.getNewGetInformation(context).getIMAC();
        String ip = getInformation.getNewGetInformation(context).getI_IP();
        mLogger.info("深大类联网...getNetworkingSZ()--->ip--"+ip+"mac"+mac);
        if(ip.equals("0.0.0.0") || mac.equals("")){
            NetworkChangeAgent.newInstance(context).getNetWorkstatePortal(new NetworkChangeAgent.OnNetworkParameterClickListener() {
                @Override
                public void onClick(String ip) {
                    mLogger.info("联网参数--->ip--"+ip+"..mac"+getInformation.getNewGetInformation(context).getIMAC());
                    if(StringUtil.getStringUtilNew().judgeNetWorkstateParameter(context,getInformation.getNewGetInformation(context).getI_IP(),ip)){
                        if(getInformation.getNewGetInformation(context).getI_IP().equals("")){
                            map.put("ip",ip);
                        }else {
                            map.put("ip",getInformation.getNewGetInformation(context).getI_IP());
                        }
                        map.put("mac",getInformation.getNewGetInformation(context).getIMAC());
                        map.put("visitType",1);
                        getNetworkingSZ1(context,map,token,id,phone,pwd,uploadDialog,state,entryPromptDialog,sessionid);

                    }else {
                        showWiFiView();
                        if(uploadDialog !=null){uploadDialog.dismiss();}
                    }
                }
            });
        }else {
            map.put("ip",ip);
            map.put("mac",mac);
            getNetworkingSZ1(context,map,token,id,phone,pwd,uploadDialog,state,entryPromptDialog,sessionid);
        }
    }

    /**
     * 深大联网
     */
    private void getNetworkingSZ1(final Context context,HashMap<String,Object> map,String token,String id, final String phone, final String pwd,
                                  final UploadDialog uploadDialog, final boolean state,final EntryPromptDialog entryPromptDialog,final String sessionid){
        String stringJson = StringUtil.getStringUtilNew().hashMapToJson(map); //参数包
        long time = StringUtil.getStringUtilNew().getTimeCurrent();
        String stirngSign = StringUtil.getStringUtilNew().getSign(Url.mStirngNetwork_verifyNet,stringJson,token,id,time);
        Map map1 = StringUtil.getStringUtilNew().getMapNew(context,stringJson,token,id,Url.mStirngNetwork_verifyNet,stirngSign,time);
        LogUtil.eE("fwq",MyApplication.getApp().getFWQString()+"");
        setNetWorkstae(true);
        String fwq = (String) SharedPreferencesUtils.getParam(context,HomeUtil.mFWQ,"");
        mLogger.info("深圳联网getNetworkingSZ--->stringJson--"+stringJson+"fwq"+fwq+"...NF_callid..."+map1.get("NF_callid"));
        GreenTreeNetworkUtil.getInstance().doPost(context,fwq,9000,map1,new RequestDataCallback() {
            @Override public void onSuccess(String response) {
                try {
                    mLogger.info("getNetworkingSZ--->onSuccess--String"+response);
                    JSONObject jsonObject = new JSONObject(response);
                    Log.e("response", "          " + response);
                    if (jsonObject.getInt("code") == 1) {
                        if (jsonObject.getInt("connect") == 1) {
                               if(sessionid.equals("")){
                                   if(uploadDialog !=null){uploadDialog.getStringShow("验证中");}
                                   HomeUtil.getHemeUtilNew().domainUtil(context,2500,new HomeUtil.OnHomeUtilClickListener() {
                                       @Override
                                       public void onItemClick(String type) {
                                           if(type.equals("ok")){
                                               if(uploadDialog !=null ){uploadDialog.dismiss();}
                                               entryPromptDialog.dismiss();
                                               szNetworkstateSuccess(context,phone,pwd,state);
                                           }else {
                                               showWiFiView();
                                               if(uploadDialog !=null){uploadDialog.dismiss();}
                                               Toast.makeText(context,"联网失败,假成功",Toast.LENGTH_SHORT).show();
                                           }
                                       }
                                   });
                               }else {
                                   if(uploadDialog !=null){uploadDialog.dismiss();}
                                   entryPromptDialog.dismiss();
                                   final RetunDialog retunDialog = new RetunDialog(context,"",6,true);
                                   retunDialog.show();
                                   HomeUtil.getHemeUtilNew().getHandlerNew().postDelayed(new Runnable() {
                                       @Override public void run() {
                                           retunDialog.dismiss();
                                     }
                                   },1000);
                               }
                        } else {
                            if(sessionid.equals("")){
                                if(uploadDialog !=null){uploadDialog.dismiss();}
                                entryPromptDialog.dismiss();
                                RetunDialog retunDialog = new RetunDialog(context,"",4,true);
                                retunDialog.show();
                            }else {
                                if(uploadDialog !=null){uploadDialog.dismiss();}
                                Toast.makeText(context, "授权失败", Toast.LENGTH_SHORT).show();
                            }
                        }
                    } else if (jsonObject.getInt("code") == -2) {
                        if(uploadDialog !=null){uploadDialog.dismiss();}
                        CrowdRetunDialog crowdRetunDialog = new CrowdRetunDialog(context);
                        crowdRetunDialog.show();
                    }else if(jsonObject.getInt("code") == 0){
                        if(uploadDialog !=null){uploadDialog.dismiss();}
                        if(sessionid.equals("")){
                            Toast.makeText(context, "联网失败", Toast.LENGTH_SHORT).show();
                        }else {
                            if(jsonObject.getInt("result") == 2){
                                Toast.makeText(context, "二维码已失败，请重新生成", Toast.LENGTH_SHORT).show();
                            }else {
                                Toast.makeText(context, "授权失败", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }else {
                        if(uploadDialog !=null){uploadDialog.dismiss();}
                        if(sessionid.equals("")){
                            Toast.makeText(context, context.getString(R.string.FWQ), Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(context, "授权失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                } catch (JSONException e) {
                    if(uploadDialog !=null){uploadDialog.dismiss();}
                    Toast.makeText(context,context.getString(R.string.FWQ),Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }

            @Override public void onFailure(Throwable error) {
                mLogger.info("getNetworkingSZ--->onFailure"+error);
                uploadDialog.dismiss();
                if(mNetworkingDialog != null){mNetworkingDialog.dismiss();}
                StringUtil.getStringUtilNew().getExceptionCode(context, error.toString());
            }
        });
    }

    /**
     * 联网返回失败时的 处理
     * @param context 上下文
     * @param uploadDialog 显示框 entryPromptDialog sz的框
     * @param state state 0深职院  1 深大
     * @param typeString szy联网的某的类型
     * @param time  szy的时间
     * @param phone sz手机
     * @param pwd sz密码
     * @param stateb sz是否要记录密码
     */
    private void networkstateFail(final Context context, final UploadDialog uploadDialog, final EntryPromptDialog entryPromptDialog,final int state,final String typeString,
                                  final String time,final String phone,final String pwd,final boolean stateb){
        LogUtil.eE("",".........1.........getmNetWorkstateFail()......"+getmNetWorkstateFail());
        if(getmNetWorkstateFail() == 0){
            if(uploadDialog !=null){
                LogUtil.eE("",".......2.............................");
                uploadDialog.getStringShow("验证中");
            }
            HomeUtil.getHemeUtilNew().domainUtil(context,600,new HomeUtil.OnHomeUtilClickListener() {
                @Override
                public void onItemClick(String type) {
                    mLogger.info("onItemClick--->验证--->String--"+type+"NetWorkstateFail.."+getmNetWorkstateFail());
                   if(uploadDialog !=null){uploadDialog.dismiss();}
                    if(entryPromptDialog !=null){ entryPromptDialog.dismiss();}
                    if(mNetworkingDialog == null){mNetworkingDialog = new NetworkingDialog(context);}
                    if(type.equals("ok")){
                        if(state == 0){
                            szuNetworkstateSuccess(context,time,"");
                        }else if(state == 1){
                            szNetworkstateSuccess(context,phone,pwd,stateb);
                        }

                    }else {
                        LogUtil.eE("","onItemClick.........2"+type+"...showShate..."+mNetworkingDialog.showShate()+".....state...."+state+"...mNetWorkstateFail.."+mNetWorkstateFail);
                        if(mNetworkingDialog.showShate()){
                            LogUtil.eE("","onItemClick.........2"+type);
                            if(mNetWorkstateFail == 0) {
                                mNetWorkstateFail++;
                                mNetworkingDialog.show();
                                mNetworkingDialog.showTVName("3","重试中");
                                if (state == 0) {
                                    getNetworking(context,uploadDialog,typeString);
                                } else if (state == 1) {
                                    getNetworkingSZ(context,phone,pwd,uploadDialog,stateb,entryPromptDialog,"");
                                }
                            }
                        }else {mNetworkingDialog.setShate(true);}
                    }
                }
            });

        }else if(getmNetWorkstateFail() == 1){
            HomeUtil.getHemeUtilNew().domainUtil(context,600,new HomeUtil.OnHomeUtilClickListener() {
                @Override public void onItemClick(String type) {
                    if(type.equals("ok")){
                        mLogger.info("onItemClick--->验证--->String--"+type+"NetWorkstateFail.."+getmNetWorkstateFail());
                        if(state == 0){
                            szuNetworkstateSuccess(context,time,"");
                        }else if(state == 1){
                            szNetworkstateSuccess(context,phone,pwd,stateb);
                        }
                    }else {
                        if(mNetworkingDialog.showShate()){
                            mNetworkingDialog.showTVName("2","重试中");
                            mNetWorkstateFail++;
                            if(state == 0){
                                getNetworking(context,uploadDialog,typeString);
                            }else if(state == 1){
                                getNetworkingSZ(context,phone,pwd,uploadDialog,stateb,entryPromptDialog,"");
                            }
                        }else {
                            mNetworkingDialog.setShate(true);
                        }
                    }
                }
            });
        }else if (getmNetWorkstateFail() == 2){
            HomeUtil.getHemeUtilNew().domainUtil(context,600,new HomeUtil.OnHomeUtilClickListener() {
                @Override public void onItemClick(String type) {
                    if(type.equals("ok")){
                        mLogger.info("onItemClick--->验证--->String--"+type+"NetWorkstateFail.."+getmNetWorkstateFail());
                        if(state == 0){
                            szuNetworkstateSuccess(context,time,"");
                        }else if(state == 1){
                            szNetworkstateSuccess(context,phone,pwd,stateb);
                        }
                    }else {
                        if(mNetworkingDialog.showShate()){
                            mNetworkingDialog.showTVName("1","重试中");
                            mNetWorkstateFail++;
                            if(state == 0){
                                getNetworking(context,uploadDialog,typeString);
                            }else if(state == 1){
                                getNetworkingSZ(context,phone,pwd,uploadDialog,stateb,entryPromptDialog,"");
                            }
                        }else {
                            mNetworkingDialog.setShate(true);
                        }
                    }
                }
            });
        }else if(getmNetWorkstateFail() == 3){
            mNetworkingDialog.getShowName("联网失败");
            showWiFiView();
            mLogger.info("networkstateFail()--->联网失败--->String--"+getmNetWorkstateFail());
            HomeUtil.getHemeUtilNew().getHandlerNew().postDelayed(new Runnable() {
                @Override public void run() {
                    mNetworkingDialog.diaLogDismiss();
                }
            },800);
        }

    }

    /**
     * 提示联网页更新 联网按钮
     */
    private void showWiFiView(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("code","WiFi");
        map.put("target",AppointTimeActivity.mShow_TV_NetWorkstate);
        EventBus.getDefault().post(StringUtil.getStringUtilNew().hashMapToJson(map));
    }

    /**
     * 深职院联网成功后的操作
     */
    private void szuNetworkstateSuccess(final Context context,final String time,final String type){


        if(SharedPreferencesUtils.getParam(context,HomeUtil.mCrashError,"").equals(CrashHandler.mCrasHandlerPath)){
            HomeUtil.getHemeUtilNew().pushError(context, Environment.getExternalStorageDirectory()+CrashHandler.mCrasHandlerPath);
        }
        EventBus.getDefault().post("SZ_NetWorkstate");
        LogUtil.eE("app3c","..type..."+type);
        if(type.equals(mRenew)){
            EventBus.getDefault().post("appintTime_ok");
        }
        HomeUtil.getHemeUtilNew().setPushAurora(context);
        if(getmNetWorkstateFail() == 0){
            final RetunDialog retunDialog = new RetunDialog(context,"",3,true);
            retunDialog.show();
            HomeUtil.getHemeUtilNew().getHandlerNew().postDelayed(new Runnable() {
                @Override public void run() {
                    retunDialog.dismiss();
                    Intent intent = new Intent(context,AlarmReceiver.class);
                    intent.putExtra("networking","updateNetwork66");
                    context.sendBroadcast(intent);
                }
            },1200);
        }else {
            mNetworkingDialog.getShowName("联网成功");
            HomeUtil.getHemeUtilNew().getHandlerNew().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mNetworkingDialog.diaLogDismiss();
                }
            },800);
        }

        mLogger.info("networking--->onSuccess--->联网成功后的地址"+MyApplication.getApp().getFWQString());
    }

    /**
     * 深大联网后的操作
     */
    private void szNetworkstateSuccess(final Context context,String phone,String pwd,boolean state){

        SharedPreferencesUtils.setParam(context, HomeUtil.mSTUDENPhone,phone);
        if(state){
            SharedPreferencesUtils.setParam(context, HomeUtil.mSTUDENPSSWORD,StringUtil.getStringUtilNew().objectString(pwd));
        }else {
            SharedPreferencesUtils.setParam(context, HomeUtil.mSTUDENPSSWORD,"");
        }

      //  MyApplication.getApp().setFWQString(Url.getUrlNew().getAppIP());
        Intent intent = new Intent(HomeActivity.mAction);
        intent.putExtra("wifi","wifiok");
        context.sendBroadcast(intent);
        EventBus.getDefault().post("SZ_NetWorkstate");
        HomeUtil.getHemeUtilNew().setPushAurora(context);
        Intent intent1 = new Intent(context,AlarmReceiver.class);
        intent1.putExtra("networking","updateNetwork66");
        context.sendBroadcast(intent1);
        if(SharedPreferencesUtils.getParam(context,HomeUtil.mCrashError,"").equals(CrashHandler.mCrasHandlerPath)){
            HomeUtil.getHemeUtilNew().pushError(context,Environment.getExternalStorageDirectory()+CrashHandler.mCrasHandlerPath);
        }

        if(getmNetWorkstateFail() == 0){
            final RetunDialog retunDialog = new RetunDialog(context,"",3,true);
            retunDialog.show();
            HomeUtil.getHemeUtilNew().getHandlerNew().postDelayed(new Runnable() {
                @Override public void run() {
                    retunDialog.dismiss();
                }
            },1000);
        }else {
            mNetworkingDialog.getShowName("联网成功");
            HomeUtil.getHemeUtilNew().getHandlerNew().postDelayed(new Runnable() {
                @Override public void run() {
                    mNetworkingDialog.diaLogDismiss();
                }
            },800);
        }

    }


    /**
     * 断..网络  type : 0 用户自动断网  ！=0 一般都是临时放行
     */
    public void getBrokenNetwork(Context context,final HomeUtil.OnHomeUtilClickListener onHomeUtilClickListener,int type) {
        String id = (String) SharedPreferencesUtils.getParam(context,HomeUtil.mUseridDB,"");
        String token = (String) SharedPreferencesUtils.getParam(context, HomeUtil.mTokenDB, "");
        if(id.equals("")){
            id = "0";
        }
        if(token.equals("")){
            token = "null";
        }
        HashMap<String,Object> map = new HashMap<>();
        map.put("ip",getInformation.getNewGetInformation(context).getI_IP());
        if(type == 0){
            map.put("userid",id);
        }else {
            map.put("userid",0);
        }
        map.put("mac",getInformation.getNewGetInformation(context).getIMAC());
        String stringJson = StringUtil.getStringUtilNew().hashMapToJson(map); //参数包
        long time = StringUtil.getStringUtilNew().getTimeCurrent();
        String stirngSign = StringUtil.getStringUtilNew().getSign(Url.mStirngpcNetwork_offNet,stringJson,token,id,time);
        Map map1 = StringUtil.getStringUtilNew().getMapNew(context,stringJson,token,id,Url.mStirngpcNetwork_offNet,stirngSign,time);
        final String fwq = (String) SharedPreferencesUtils.getParam(context ,HomeUtil.mFWQ,"");
        mLogger.info("getBrokenNetwork()断网--->"+"fwq--"+fwq+"stringJson"+stringJson+"token"+token+"id"+id+"...NF_callid..."+map1.get("NF_callid"));
        GreenTreeNetworkUtil.getInstance().doPost(context,fwq,5000,map1,new RequestDataCallback() {
            @Override public void onSuccess(String response){
                try {
                    mLogger.info("getBrokenNetwork()--->onSuccess()--->"+"String--"+response);
                    JSONObject jsonObject = new JSONObject(response);
                    if(jsonObject.getInt("code") == 1){
                        MyApplication.getApp().setFWQString(fwq);
                        if(onHomeUtilClickListener !=null){
                            onHomeUtilClickListener.onItemClick("ok");
                        }
                        EventBus.getDefault().post("update_head_view");
                    }else {
                        if(onHomeUtilClickListener !=null){
                            onHomeUtilClickListener.onItemClick("no");
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    if(onHomeUtilClickListener !=null){
                        onHomeUtilClickListener.onItemClick("no");
                    }
                }
            }

            @Override
            public void onFailure(Throwable error) {
                if(onHomeUtilClickListener !=null){
                    onHomeUtilClickListener.onItemClick("no");
                }
                mLogger.info("getBrokenNetwork()断网--->失败"+"onFailure（）--Throwable.."+error);
            }
        });

    }

    /**
     * 临时放行网络
     */
    public void showNetworking(Context context,final NetworkingOnClickListener networkingOnClickListener){
        LogUtil.eE("","judgeWhetherNetworking.........4");
        String id = (String) SharedPreferencesUtils.getParam(context,HomeUtil.mUseridDB,"");
        String token = (String) SharedPreferencesUtils.getParam(context, HomeUtil.mTokenDB, "");
        HashMap<String,Object> mMap = new HashMap<>();
        if(id.equals("")){
            id = "0";
        }
        if(token.equals("")){
            token = "null";
        }
        mMap.put("userid",id);
        mMap.put("ip",getInformation.getNewGetInformation(context).getI_IP());
        mMap.put("userMac",getInformation.getNewGetInformation(context).getIMAC());
        mMap.put("siteAreaid",MyApplication.getApp().getSiteAreaid());
        mMap.put("visitType",3);
        String stringJson = StringUtil.getStringUtilNew().hashMapToJson(mMap); //参数包
        long time = StringUtil.getStringUtilNew().getTimeCurrent();
        String stirngSign = StringUtil.getStringUtilNew().getSign(Url.mStirngpcNetwork_tempNet,stringJson,token,id,time);
        Map map = StringUtil.getStringUtilNew().getMapNew(context,stringJson, token,id,Url.mStirngpcNetwork_tempNet,stirngSign,time);
        mLogger.info("showNetworking临时联网--->FWQ--->"+MyApplication.getApp().getFWQString()+"..stringJson.."+stringJson+"...NF_callid..."+map.get("NF_callid"));
        GreenTreeNetworkUtil.getInstance().doPost(context,MyApplication.getApp().getFWQString(),1,map,new RequestDataCallback() {
            @Override public void onSuccess(String response) {
                try {
                    LogUtil.eE("","judgeWhetherNetworking.........6"+response);
                    mLogger.info("showNetworking--->onSuccess--->"+"String--"+response);
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.getInt("code") == 1) {
                        if(jsonObject.getInt("connect") == 1){
                            networkingOnClickListener.onItemClick(true);
                            Url.getUrlNew().setNETWORKING_ATATE(1);
                        }else {networkingOnClickListener.onItemClick(false);}
                    }else {
                        networkingOnClickListener.onItemClick(false);
                        Url.getUrlNew().setNETWORKING_ATATE(0);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    networkingOnClickListener.onItemClick(false);
                }
            }

            @Override public void onFailure(Throwable error) {
                Url.getUrlNew().setNETWORKING_ATATE(0);
                networkingOnClickListener.onItemClick(false);
                LogUtil.eE("","judgeWhetherNetworking.........7");
                mLogger.info("showNetworking临时联网--->失败"+"Throwable--"+error);
            }
        });
    }

    /**
     * 会员预约时间内连接网络
     */
    public void networkPlanInNet(final Context context){
        String id = (String) SharedPreferencesUtils.getParam(context,HomeUtil.mUseridDB,"");
        String token = (String) SharedPreferencesUtils.getParam(context, HomeUtil.mTokenDB, "");
        HashMap<String,Object> mMap = new HashMap<>();
        mMap.put("ip",getInformation.getNewGetInformation(context).getI_IP());
        mMap.put("userid",id);
        mMap.put("mac",getInformation.getNewGetInformation(context).getIMAC());
        String stringJson = StringUtil.getStringUtilNew().hashMapToJson(mMap); //参数包
        long time = StringUtil.getStringUtilNew().getTimeCurrent();
        String stirngSign = StringUtil.getStringUtilNew().getSign(Url.getUrlNew().network_planInNet,stringJson,token,id,time);
        Map map = StringUtil.getStringUtilNew().getMapNew(context,stringJson,token,id,Url.getUrlNew().network_planInNet,stirngSign,time);
        String fwq = (String) SharedPreferencesUtils.getParam(context,HomeUtil.mFWQ,"");
        mLogger.info("networkPlanInNet--->FWQ--->"+fwq);
        LogUtil.eE("","judgeWhetherNetworking.........5");
        GreenTreeNetworkUtil.getInstance().doPost(context,fwq,1,map,new RequestDataCallback() {
            @Override public void onSuccess(String response) {
                try {
                    mLogger.info("networkPlanInNet-->onSuccess()"+response);
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.getInt("code") == 1) {
                           if(jsonObject.getInt("connect") == 1 ){
                               HashMap<String,Object> hashMap = new HashMap<>();
                               hashMap.put("code","WiFi");
                               hashMap.put("target", AppointTimeActivity.mNetWorkstate);
                               hashMap.put("sign","chonglian");
                               String stringJson = StringUtil.getStringUtilNew().hashMapToJson(hashMap); //参数包
                               EventBus.getDefault().post(stringJson);
                               Intent intent = new Intent(context,AlarmReceiver.class);
                               intent.putExtra("networking","updateNetwork66");
                               context.sendBroadcast(intent);
                           }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override public void onFailure(Throwable error) {
                mLogger.info("showNetworking--->失败"+"Throwable--"+error);
            }
        });
    }

    /**
     * 授权pc联网  套餐
     */
    public void ssidPCNetWorkstate(final Context context,String sessionid,final UploadDialog uploadDialog){
        uploadDialog.show();
        String id = (String) SharedPreferencesUtils.getParam(context,HomeUtil.mUseridDB,"");
        String token = (String) SharedPreferencesUtils.getParam(context, HomeUtil.mTokenDB, "");
        HashMap<String, Object> map = new HashMap<>();
        map.put("sessionid",sessionid);
        map.put("userid",id);
        map.put("planTime","720");
        String stringJson = StringUtil.getStringUtilNew().hashMapToJson(map); //参数包
        final long time = StringUtil.getStringUtilNew().getTimeCurrent();
        String stirngSign = StringUtil.getStringUtilNew().getSign(Url.getUrlNew().PCNetQr_pcQrselfNet,stringJson,token,id,time);
        Map map1 = StringUtil.getStringUtilNew().getMapNew(context,stringJson,token,id,Url.getUrlNew().PCNetQr_pcQrselfNet,stirngSign,time);
        String fwq = (String) SharedPreferencesUtils.getParam(context,HomeUtil.mFWQ,"");
        mLogger.info("ssidPCNetWorkstate()-->"+"...stringJson..."+stringJson+"...NF_callid..."+map1.get("NF_callid")+"..fwq.."+fwq);
        GreenTreeNetworkUtil.getInstance().doPost(context,fwq,1,map1, new RequestDataCallback() {
            @Override
            public void onSuccess(String response) {
                try {
                    mLogger.info("ssidPCNetWorkstate()--->String--"+response);
                    JSONObject json = new JSONObject(response);
                    if(json.getInt("code") == 1){
                        uploadDialog.dismiss();
                        int connect  = json.getInt("connect");
                        if(connect == 1){
                            final RetunDialog retunDialog = new RetunDialog(context,"",6,true);
                            retunDialog.show();
                            HomeUtil.getHemeUtilNew().getHandlerNew().postDelayed(new Runnable() {
                                @Override public void run() {
                                    retunDialog.dismiss();
                                }
                            },1000);
                        }else if(connect == -1){
                            Toast.makeText(context,context.getString(R.string.FWQ),Toast.LENGTH_SHORT).show();
                        }else if(connect == -2){
                            Toast.makeText(context,"服务器超时,请重新扫码",Toast.LENGTH_SHORT).show();
                        }else if(connect == 2){
                            Toast.makeText(context,"您现在还没有权限联网",Toast.LENGTH_SHORT).show();
                        }else if(connect == 0){
                            Toast.makeText(context,"认证异常,可以重新扫码",Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(context,"授权失败,可以重新扫码",Toast.LENGTH_SHORT).show();
                        }
                    }else if(json.getInt("code") == 0){
                        uploadDialog.dismiss();
                        int reason  = json.getInt("reason");
                        if(reason == 1 || reason == 3){
                            Toast.makeText(context,"信息过时，请重新扫码",Toast.LENGTH_SHORT).show();
                        }else if(reason == 5){
                            Toast.makeText(context,"时长不足，请先充值",Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(context,"授权失败，请重新扫码",Toast.LENGTH_SHORT).show();
                        }
                    }else if(json.getInt("code") == -1){
                        uploadDialog.dismiss();
                        Toast.makeText(context,context.getString(R.string.FWQ),Toast.LENGTH_SHORT).show();
                    }else if (json.getInt("code") == -2) {
                        uploadDialog.dismiss();
                        CrowdRetunDialog crowdRetunDialog = new CrowdRetunDialog(context);
                        crowdRetunDialog.show();
                    }else {
                        uploadDialog.dismiss();
                        Toast.makeText(context,"授权失败",Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    uploadDialog.dismiss();
                    Toast.makeText(context,"授权失败",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Throwable error) {
                mLogger.info("ssidPCNetWorkstate()--->error--"+error);
                uploadDialog.dismiss();
                Toast.makeText(context,"授权失败",Toast.LENGTH_SHORT).show();
                StringUtil.getStringUtilNew().getExceptionCode(context,error.toString());
            }
        });
    }

    /**
     * 检查网络
     */
    public void getNetWorkState(Context context,final NetworkingOnClickListener networkingOnClickListener){
        String stringJson ="{}"; //参数包
        long time = StringUtil.getStringUtilNew().getTimeCurrent();
        String stirngSign = StringUtil.getStringUtilNew().getSign(Url.mStirngVersion_isAlive,stringJson,"null","0",time);
        Map map = StringUtil.getStringUtilNew().getMapNew(context,stringJson,"null","0",Url.mStirngVersion_isAlive,stirngSign,time);
        mLogger.info("agentIP()...fwq.."+context);
        GreenTreeNetworkUtil.getInstance().doPost(context,mFWQIP,2000,map,new RequestDataCallback() {
            @Override public void onSuccess(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.getInt("code") == 1) {
                        networkingOnClickListener.onItemClick(true);
                    }else {
                        networkingOnClickListener.onItemClick(true);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    networkingOnClickListener.onItemClick(false);
                }
            }

            @Override public void onFailure(Throwable error) {
                networkingOnClickListener.onItemClick(false);
            }});
    }

    public int getmNetWorkstateFail() {
        return mNetWorkstateFail;
    }

    public void setmNetWorkstateFail(int mNetWorkstateFail) {
        this.mNetWorkstateFail = mNetWorkstateFail;
    }

    public boolean isNetWorkstae() {
        return isNetWorkstae;
    }

    public void setNetWorkstae(boolean netWorkstae) {
        isNetWorkstae = netWorkstae;
    }

    /**
     * 返回临时放行的结果
     */
    public interface NetworkingOnClickListener {
        void onItemClick(boolean state);
    }
}
